/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import systole.domain.clinicalInformation.Surgery;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.SurgerySyncBroker;
import systole.synchronization.remote.entities.SurgeryRemote;

/**
 *
 * @author Juan Manuel
 */
public class SurgerySyncBrokerDB extends BrokerDB implements SurgerySyncBroker {

    public void saveSurgeryRemote(SurgeryRemote surgeryRemote) throws ExceptionDAO {
        try {
            this.logger.logDebug("saving remote surgery");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(surgeryRemote);
            this.logger.logDebug("remote surgery saved");
        } catch (HibernateException e) {
            this.logger.logError("error on save surgery remote, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron guardar los cambios", e.fillInStackTrace());
        }
    }

    public List<SurgeryRemote> getSurgeriesToUpdate() throws ExceptionDAO {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Surgery> getSurgeriesToUpload() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting surgery to upload");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            String hql = "select s from SurgeryRemote sr right join sr.surgery s where sr=null";
            Query query = currentSession.createQuery(hql);
            @SuppressWarnings("unchecked")
            List<Surgery> list = query.list();
            this.logger.logDebug("get successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get surgery to upload, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los familiares", e.fillInStackTrace());
        }
    }

    public Surgery getSurgeryByRemoteId(int remoteId) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting surgery by remote id");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    SurgeryRemote.class).add(Restrictions.eq("remoteId", remoteId)).setMaxResults(1);
            SurgeryRemote surgeryRemote = (SurgeryRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (surgeryRemote != null ? surgeryRemote.getSurgery() : null);
        } catch (HibernateException e) {
            this.logger.logError("error on get surgery by remote id, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las cirugias", e.fillInStackTrace());
        }
    }

    public SurgeryRemote getSurgeryRemoteBySurgery(Surgery surgery) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting surgery remote by local surgery");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    SurgeryRemote.class).add(Restrictions.eq("surgery", surgery)).setMaxResults(1);
            SurgeryRemote surgeryRemotes = (SurgeryRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return surgeryRemotes;
        } catch (HibernateException e) {
            this.logger.logError("error on get remote surgery by surgery, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las cirugias", e.fillInStackTrace());
        }
    }

    public int getLastRemoteSurgeryIdSynchronized() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting last remote surgery sync");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    SurgeryRemote.class).addOrder(Order.desc("remoteId")).setMaxResults(1);
            SurgeryRemote surgeryRemote = (SurgeryRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (surgeryRemote != null ? surgeryRemote.getRemoteId() : -1);
        } catch (HibernateException e) {
            this.logger.logError("error on get last remote surgery synchronized, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las cirugias", e.fillInStackTrace());
        }
    }
}
